﻿using System.Collections;
using System.Collections.Generic;
using Business.Common.QueryModel;
using Business.DataAccess.Repository.Wms;
using Business.Domain.Wms;
using Framework.Core.Collections;

namespace Business.Component
{
    public class SkuManager
    {
        public static Pack GetDefaultPack(int skuId)
        {
            var repository = new PackRepository();

            var query = new Query();
            query.Criteria.Add(new Criterion("SkuId", CriteriaOperator.Equal, skuId));

            IList<Pack> packs = repository.GetListByQuery(query);
            foreach (Pack pack in packs)
            {
                if (pack.DefaultPack)
                    return pack;
            }

            return null;
        }

        public static SkuManagement GetSkuManagement(int skuId)
        {
            var query = new Query();
            query.Criteria.Add(new Criterion("SkuId", CriteriaOperator.Equal, skuId));

            var skuManagementRepository = new SkuManagementRepository();
            SkuManagement skuManagement = skuManagementRepository.GetByQuery(query);

            if (skuManagement != null)
                return skuManagement;
            else
            {
                var skuRepository = new SkuRepository();
                Sku sku = skuRepository.Get(skuId);

                var query1 = new Query();
                query1.Criteria.Add(new Criterion("CategoryId", CriteriaOperator.Equal, sku.CategoryId));

                var categoryManagementRepository = new CategoryManagementRepository();
                CategoryManagement categoryManagement = categoryManagementRepository.GetByQuery(query1);

                return new SkuManagement
                           {
                               AbcType = categoryManagement.AbcType,
                               BarcodeManagement = categoryManagement.BarcodeManagement,
                               ContainerManagement = categoryManagement.ContainerManagement,
                               CreateTime = categoryManagement.CreateTime,
                               CreateUser = categoryManagement.CreateUser,
                               EditTime = categoryManagement.EditTime,
                               EditUser = categoryManagement.EditUser,
                               IsActive = categoryManagement.IsActive,
                               IsBigItem = categoryManagement.IsBigItem,
                               IsHeavyItem = categoryManagement.IsHeavyItem
                           };
            }
        }

        private static ArrayList GetSkuProperties(int skuId)
        {
            var query = new Query();
            query.Criteria.Add(new Criterion("SkuId", CriteriaOperator.Equal, skuId));

            var skuPropertyRepository = new SkuPropertyRepository();
            ArrayList skuProperties = CollectionHelper.ToArrayList(skuPropertyRepository.GetListByQuery(query));
            var comparer = new SkuPropertyComparer();
            if (skuProperties.Count > 0)
            {
                skuProperties.Sort(comparer);
                return skuProperties;
            }
            else
            {
                var skuRepository = new SkuRepository();
                Sku sku = skuRepository.Get(skuId);

                var query1 = new Query();
                query1.Criteria.Add(new Criterion("CategoryId", CriteriaOperator.Equal, sku.CategoryId));

                var categoryPropertyRepository = new CategoryPropertyRepository();
                IList<CategoryProperty> categoryProperties = categoryPropertyRepository.GetListByQuery(query1);

                skuProperties = new ArrayList();
                foreach (CategoryProperty item in categoryProperties)
                {
                    skuProperties.Add(new SkuProperty
                                          {
                                              Priority = item.Priority,
                                              PropertyId = item.PropertyId,
                                              SkuId = skuId
                                          });
                }

                skuProperties.Sort(comparer);
                return skuProperties;
            }
        }

        public static List<BatchProperty> GetBatchProperty(int skuId)
        {
            ArrayList skuProperties = GetSkuProperties(skuId);

            var repository = new BatchPropertyRepository();
            var resultList = new List<BatchProperty>();
            foreach (SkuProperty item in skuProperties)
            {
                int propertyId = item.PropertyId;
                BatchProperty batchProperty = repository.Get(propertyId);
                resultList.Add(batchProperty);
            }

            return resultList;
        }

        #region Nested type: SkuPropertyComparer

        private class SkuPropertyComparer : IComparer
        {
            #region IComparer Members

            public int Compare(object x, object y)
            {
                return ((SkuProperty) x).Priority - ((SkuProperty) y).Priority;
            }

            #endregion
        }

        #endregion
    }
}